<!DOCTYPE html>
<html>
<head><meta charset="utf-8" />
<title>projectFDM1D</title><script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.10/require.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>

<style type="text/css">
/* Overrides of notebook CSS for static HTML export */
body {
  overflow: visible;
  padding: 8px;
}
div#notebook {
  overflow: visible;
  border-top: none;
}@media print {
  div.cell {
    display: block;
    page-break-inside: avoid;
  } 
  div.output_wrapper { 
    display: block;
    page-break-inside: avoid; 
  }
  div.output { 
    display: block;
    page-break-inside: avoid; 
  }
}
</style>

<!-- Custom stylesheet, it must be in the parent directory as the html file -->
<link rel="stylesheet" type="text/css" media="all" href="../doc.css" />
<link rel="stylesheet" type="text/css" media="all" href="doc.css" />

<!-- Loading mathjax macro -->
<!-- Load mathjax -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS_HTML"></script>
    <!-- MathJax configuration -->
    <script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [ ['$','$'], ["\\(","\\)"] ],
            displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
            processEscapes: true,
            processEnvironments: true
        },
        // Center justify equations in code and markdown cells. Elsewhere
        // we use CSS to left justify single line equations in code cells.
        displayAlign: 'center',
        "HTML-CSS": {
            styles: {'.MathJax_Display': {"margin": 0}},
            linebreaks: { automatic: true }
        }
    });
    </script>
    <!-- End of mathjax configuration --></head>
<body>
  <div tabindex="-1" id="notebook" class="border-box-sizing">
    <div class="container" id="notebook-container">

<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h1 id="Project:-1-D-Finite-Difference-Method">Project: 1-D Finite Difference Method<a class="anchor-link" href="#Project:-1-D-Finite-Difference-Method">&#182;</a></h1><p>The purpose of this project is to introduce the finite difference method for solving differential equations in 1-D. Consider 1-D Poisson equation in $(0,1)$ with Dirichlet boundary condition</p>
$$ - u'' = f \,\; {\rm in }\, (0,1), \quad u(0) = a, u(1) = b. $$
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Step-1:-Generate-a-grid">Step 1: Generate a grid<a class="anchor-link" href="#Step-1:-Generate-a-grid">&#182;</a></h2><p>Generate a vector representing a uniform grid with size <code>h</code> of (0,1).</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-matlab"><pre><span></span><span class="n">N</span> <span class="p">=</span> <span class="mi">10</span><span class="p">;</span>
<span class="n">x</span> <span class="p">=</span> <span class="nb">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="n">N</span><span class="p">);</span>
<span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="s">&#39;b.&#39;</span><span class="p">,</span><span class="s">&#39;Markersize&#39;</span><span class="p">,</span><span class="mi">16</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt"></div>




<div class="output_png output_subarea ">
<img src="
B3RJTUUH4QgTBDQBqDS6igAAACR0RVh0U29mdHdhcmUATUFUTEFCLCBUaGUgTWF0aFdvcmtzLCBJ
bmMuPFjdGAAAACJ0RVh0Q3JlYXRpb24gVGltZQAxOS1BdWctMjAxNyAxMjo1MjowMOsuN7cAACAA
SURBVHic7d1/VFR1/sfxz8jIEIU5p6YGbJM8xybLE+mIo0O0zFRuxwPZ7jkd6Y/KzTqxxhzXjbSE
Wu3U1w50+grYtss55ddaiX4ejxyz9Vu6YO52WMfOqTgrTMaQLIGwOEDCsPy43z9mm2aV0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++OO5ubmxKKSz
BnjooYcOHjwY9f2eY4CxsbE777xz9+7dmqYNDg7efvvtf/vb3+KcIVJXV9eKFSu+/vrreAY4derU
vHnzjhw5omnat99+e9NNN4W245nhk08+WbJkSVdXl6Zpx44dW7hw4VdffRX1DKeJw1EoKqbaJbuG
hgar1epwOJRSFovF7XbX19fru0en0/nYY4/94he/0CvD0aNHnU7n7NmzlVJpaWnXXntta2trPAP0
9vY+9NBDV199tVLKZrMlJCSMjo5GN8BZM4TU1taazWa73R71vZ9LgKNHj95www1tbW0xuk41cYAj
R44YDIa8vDxN05KSkvbt2xeL78M5/gYcHR0tKCj4zW9+c91118UzgMFgMBqNobfijBkzpk+fnpiY
GN0AZ83g9XqXLl165ZVXKqXmzJlz8803Hzx4MOoZThOHo1BUTLVC6uzsTE1NDS+tVuuJEyf03WNm
Zuatt94a6gNdMuTl5b388suhbb/ff+zYsYyMjHgGWLhwYUFBwXfffff+++8/+uijDzzwQFpaWnQD
nDWDUqq9vX3Hjh2PP/541Hd9LgH++c9/9vb2rlq16oEHHsjMzIzF39U2cYDm5mabzbZp0yaHw7Fk
yZIY/WVx5/gb8O23305JSbntttviHOCSSy5Zv379mjVrfve7361evTovL+/GG2+Mc4aZM2e2t7eH
tjVN6+joCC9jJw5HoaiYaoU0MjKSkJAQXhqNxuHh4Sm2x/PO4PV6H3jggYKCgjlz5sQ/wODg4Oef
f97f39/b23vq1KnoBjhrhrGxsSeffLK4uDgpKSnquz6XAL29vXfddddrr722f//+mpqaV1999S9/
+Us8AwQCgf3791ut1oMHD27fvv3tt9/evXt3dAOcNUNIMBisqKjweDxR3/tZA4yOjn7zzTe9vb3t
7e1DQ0NtbW09PT1xznDXXXcdPXr0pZde+uyzz5599tlTp04NDQ1FPcMkNdUKyWQyRV4PGRwcNJlM
U2yP55FheHh4y5Ytv/71r0tKSn71q1/FP4BSymKxbNq06f33329ubq6trY1zNVYPtQAAA8NJREFU
hh07dpjN5ksvvbS5ufnkyZNdXV1R/1g6cYA5c+a8+OKLV111lVLqpptuWrZs2aeffhrPAElJSZdf
fvmjjz5qMpnmzZt37733HjhwILoBzpohZP/+/cnJyQsXLoz63s8awOv17tmzZ/fu3c8999x77703
MjLy1ltvxTnDVVdd9eabb37zzTcvvfTSdddd53a7r7jiiqhnmKQmxx+ueu5mzZoV+TcktbS0zJ07
d4rt8TwyeDweo9G4Z8+eGTNmxD/A73//+66urqefflopNW3atPnz5x87dizOGQKBQEtLS1FRkVKq
o6MjMTFxeHi4uLg4bgEaGhoCgcCyZctCy7GxsajfSJs4QHp6emJiosFgCC0TExNjcSfvXH471NbW
3nXXXVHf9bkE8Pl8aWlpKSkpSimDwWC323V5K46NjW3dujW0zM/Pv//++6OeYZKaamdImZmZQ0ND
NTU1SqnGxsa6ujqXy6XLHqurq71eb0x3fY4ZPvroo7a2tvLy8hi10VkDzJ07d9euXY2NjUqp48eP
f/zxx4sWLYpzhnXr1u3+3vLly1evXh3dNjprgGAwuHnz5ra2NqXUV199tX///ttvvz2eAZYuXTow
MLB3716lVE9Pz65du3JycqIb4KwZQjOff/551O9inmOAjIyML7744tChQ0qpnp6evXv3xuLJjokz
JCQkPPTQQ3//+9+VUp988onP5+OvMviB3o/5Rd/HH3/scDiWLl06f/78V155Ra89OhyOl156KTzz
yiuvxPSBywkybNq06YYbbrgpwp/+9Kd4BtA07aWXXpo/f/5Pf/pTu91eXl4e9b2fS4aw3/72tzH6
/5AmDlBeXr5w4cI77rhj6dKlb775ZvwDHDly5Gc/+1lOTs7ixYv/67/+a2RkJP4ZAoHA9ddfH9MH
nScO8Prrr4f+b7AFCxZs2rRpdHQ0/hneeeedO++887bbbrv11ltDT4fHR6yPQhduav7hqpqmdXd3
m81mozFO1yTjv0eBGSYOMDo62t3dfeWVV0be741zhjiYOMDIyMjJkyctFoteAZRSgUAgJSXlYv4p
jI2Nhb46ffp0vTIopU6ePHmOf/n1xWNqFhIAYNKZaveQAACTFIUEABCBQgIAiEAhAQBEoJAAACJQ
SAAAESgkAIAIFBIAQAQKCQAgAoUEABCBQgIAiEAhAQBEoJAAACJQSAAAESgkAIAIFBIAQAQKCQAg
AoUEABCBQgIAiEAhAQBEoJAAACJQSAAAESgkAIAIFBIAQAQKCQAgAoUEABDh/wBK5HQ3UswAywAA
AABJRU5ErkJggg==
"
>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Step-2:-Generate-A-Matrix-Equation">Step 2: Generate A Matrix Equation<a class="anchor-link" href="#Step-2:-Generate-A-Matrix-Equation">&#182;</a></h2><p>Based on a grid, the function $u$ is discretized by a vector <code>u</code>. The
derivative $u', u''$ are approximated by centeral finite difference:</p>
$$ u'(x_i) \approx \frac{u(i+1) - u(i-1)}{2h}$$$$ u''(x_i) \approx \frac{u(i-1) - 2u(i) + u(i+1)}{h^2}$$<p></p>
<p>The equation $-u''(x) = f(x)$ is discretized at $x_i, i=1,...,N$ as</p>
$$ \frac{-u(i-1) + 2u(i) - u(i+1)}{h^2} \quad = f(i),$$<p>where $f(i) = f(x_i)$. These linear equations can be written as a matrix
equation <code>A*u = f</code>, where <code>A</code> is a tri-diagonal matrix <code>(-1,2,-1)/h^2</code>.</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-matlab"><pre><span></span><span class="n">n</span> <span class="p">=</span> <span class="mi">5</span><span class="p">;</span>
<span class="n">e</span> <span class="p">=</span> <span class="nb">ones</span><span class="p">(</span><span class="n">n</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span>
<span class="n">A</span> <span class="p">=</span> <span class="n">spdiags</span><span class="p">([</span><span class="n">e</span> <span class="o">-</span><span class="mi">2</span><span class="o">*</span><span class="n">e</span> <span class="n">e</span><span class="p">],</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span><span class="mi">1</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">n</span><span class="p">);</span>
<span class="n">display</span><span class="p">(</span><span class="n">full</span><span class="p">(</span><span class="n">A</span><span class="p">));</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt"></div>


<div class="output_subarea output_stream output_stdout output_text">
<pre>
ans =

    -2     1     0     0     0
     1    -2     1     0     0
     0     1    -2     1     0
     0     0     1    -2     1
     0     0     0     1    -2

</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>We use <code>spdiags</code> to speed up the generation of the matrix since the matrix is sparse. Compare <code>diag</code> and <code>spdiags</code>.</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Stpe-3:-Modify-the-Matrix-Equation-to-Impose-Boundary-Conditions">Stpe 3: Modify the Matrix Equation to Impose Boundary Conditions<a class="anchor-link" href="#Stpe-3:-Modify-the-Matrix-Equation-to-Impose-Boundary-Conditions">&#182;</a></h2><p>The discretization fails at boundary vertices since no nodes outside the
interval. Howevery the boundary value is given by the problem: <code>u(1) = a,
u(N) = b</code>.</p>
<p>These two equations can be included in the matrix by changing <code>A(1,:) =
[1, 0 ..., 0]</code> and <code>A(:,N) = [0, 0, ..., 1]</code> and <code>f(1) = a/h^2, f(N) =
b/h^2</code>.</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-matlab"><pre><span></span><span class="n">A</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span> <span class="p">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="n">A</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span> <span class="p">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="n">A</span><span class="p">(</span><span class="n">n</span><span class="p">,</span><span class="n">n</span><span class="p">)</span> <span class="p">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="n">A</span><span class="p">(</span><span class="n">n</span><span class="p">,</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="p">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="n">display</span><span class="p">(</span><span class="n">full</span><span class="p">(</span><span class="n">A</span><span class="p">));</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt"></div>


<div class="output_subarea output_stream output_stdout output_text">
<pre>
ans =

     1     0     0     0     0
     1    -2     1     0     0
     0     1    -2     1     0
     0     0     1    -2     1
     0     0     0     0     1

</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Step-4:-Test-the-Correctness">Step 4: Test the Correctness<a class="anchor-link" href="#Step-4:-Test-the-Correctness">&#182;</a></h2>
<pre><code>[u,x] = poisson1D(0,1,5);
plot(x,sin(x),x,u,'r*');
legend('exact solution','approximate solution')

</code></pre>
<p>Choose an exact solution $u=\sin(x)$ and run your code to show the computed solution fits well on the curve of the true solution.</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Step-5:-Check-the-Convergence-Rate">Step 5: Check the Convergence Rate<a class="anchor-link" href="#Step-5:-Check-the-Convergence-Rate">&#182;</a></h2>
<pre><code>err = zeros(4,1);
h = zeros(4,1);
for k = 1:4
    [u,x] = poisson1D(0,1,2^k+1);
    uI = sin(x);
    err(k) = max(abs(u-uI));
    h(k) = 1/2^k;
end
display(err)
loglog(h,err,h,h.^2);
legend('error','h^2');
axis tight;

</code></pre>
<p>Change mesh size and show the convergence rate is second order, i.e. $h^2$.</p>

</div>
</div>
</div>
    </div>
  </div>
</body>

 


</html>
